﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Jack.IData
{
    //@jack mark 数据解析
    public class DataResolve
    {
        public string DbTableName<T>(T model)
        {
            string dbName = string.Empty;
            TableAttribute attr = null;
            object[] attributes = model.GetType().GetCustomAttributes(typeof(TableAttribute), true);
            if (attributes.Length > 0)
            {
                attr = (TableAttribute)attributes[0];
            }
            if (attr != null)
                dbName = attr.TableName;
            return dbName;
        }

        public string InitDbColumns<T>(T model)
        {
            StringBuilder commandBuilder = new StringBuilder();
            PropertyAttribute attr = null;
            foreach (PropertyInfo property in model.GetType().GetProperties())
            {
                object[] attributes = property.GetCustomAttributes(typeof(PropertyAttribute), true);
                if (attributes.Length > 0)
                {
                    attr = (PropertyAttribute)attributes[0];
                }
                commandBuilder.Append(attr.DbColumnName + ",");
            }
            return commandBuilder.ToString().Substring(0, commandBuilder.ToString().Length - 1);
        }   
    }
}
